package io.gitee.dqcer.mcdull.uac.provider.web.controller.system;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.gitee.dqcer.mcdull.uac.provider.common.config.RuoYiConfig;
import io.gitee.dqcer.mcdull.uac.provider.common.utils.file.FileUploadUtils;
import io.gitee.dqcer.mcdull.uac.provider.system.domain.SysOss;
import io.gitee.dqcer.mcdull.uac.provider.system.domain.vo.SysOssVO;
import io.gitee.dqcer.mcdull.uac.provider.system.service.ISysFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.gitee.dqcer.mcdull.uac.provider.common.annotation.Log;
import io.gitee.dqcer.mcdull.uac.provider.common.core.controller.BaseController;
import io.gitee.dqcer.mcdull.uac.provider.common.core.domain.AjaxResult;
import io.gitee.dqcer.mcdull.uac.provider.common.enums.BusinessType;
import io.gitee.dqcer.mcdull.uac.provider.common.utils.poi.ExcelUtil;
import io.gitee.dqcer.mcdull.uac.provider.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件Controller
 *
 * @author ruoyi
 * @date 2023-12-09
 */
@RestController
@RequestMapping("/system/file")
public class SysOssController extends BaseController {
    @Resource
    private ISysFileService sysFileService;

    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOss sysOss) {
        startPage();
        List<SysOssVO> list = sysFileService.selectSysFileList(sysOss);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOss sysOss) {
        List<SysOssVO> list = sysFileService.selectSysFileList(sysOss);
        ExcelUtil<SysOssVO> util = new ExcelUtil<SysOssVO>(SysOssVO.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId) {
        return success(sysFileService.selectSysFileByFileId(fileId));
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOss sysOss) {
        return toAjax(sysFileService.insertSysFile(sysOss));
    }

    @Log(title = "上传文件", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        String filePath = RuoYiConfig.getUploadPath();
        String fileName = FileUploadUtils.upload(filePath, file);
        sysFileService.upload(file, fileName);
        return success();
    }


    /**
     * 修改文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOss sysOss) {
        return toAjax(sysFileService.updateSysFile(sysOss));
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) {
        return toAjax(sysFileService.deleteSysFileByFileIds(fileIds));
    }
}
